package com.daidai.base

import android.app.Activity
import android.content.Context
import android.os.Build
import android.os.Bundle
import android.view.View
import android.view.ViewGroup
import android.view.Window
import android.view.WindowManager
import androidx.appcompat.app.AppCompatActivity

abstract class BaseActivity: AppCompatActivity() {
    lateinit var mContext:Context
    override fun onCreate(savedInstanceState: Bundle?) {
        // 1. 设置状态栏透明
        // 2. 设置全屏,让背景图片可以填充状态栏
        setStatusBarColor(this,android.R.color.transparent)
        setAndroidNativeLightStatusBar(this,isDarkMode())
        super.onCreate(savedInstanceState)
        setContentView(getContentViewResId())
        process()
        if(hideActionBar()){
            supportActionBar?.hide()
        }
        mContext = this

        initView()
        initData()
    }

    abstract fun getContentViewResId():Int
    abstract fun initView()
    abstract fun initData()

    fun findBtn(id: Int) = findViewById<View>(id)

    private fun process() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            val content = (findViewById<View>(android.R.id.content) as ViewGroup).getChildAt(0)
            if (content != null && !isUseFullScreenMode()) {
                content.fitsSystemWindows = true
            }
        }
    }

    /**
     * 来自于:https://www.jianshu.com/p/7392237bc1de
     * 修改状态栏颜色，支持4.4以上版本
     * @param activity
     * @param colorId
     */
    private fun setStatusBarColor(activity: Activity, colorId: Int) {
        val window: Window = activity.window
        window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS)
        window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS)
        window.statusBarColor = activity.resources.getColor(colorId, null)
    }

    private fun setAndroidNativeLightStatusBar(activity: Activity, dark: Boolean) {
        val decor: View = activity.window.decorView
        if (dark) {
            decor.systemUiVisibility = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN or View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR
        } else {
            decor.systemUiVisibility = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN or View.SYSTEM_UI_FLAG_LAYOUT_STABLE
        }
    }

    //  是否使用全屏模式
    open fun isUseFullScreenMode() = false
    open fun isDarkMode() = true
    open fun hideActionBar() = true
}